const xml2js = require('xml2js');

module.exports = () => {
    return async function xmlParser(ctx, next) {
        if (ctx.is('text/xml') || ctx.is('application/xml')) {
            // 等待将请求的XML数据解析为JavaScript对象
            ctx.request.body = await parseXML(ctx.request.body);
        }
        await next();
    };
};

function parseXML(xmlString) {
    return new Promise((resolve, reject) => {
        xml2js.parseString(xmlString, (err, result) => {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
}
